<?php

/*
 * Author: Quan Van Sinh
 * Email : qvsinh@gmail.com
 * Mobile: 0972405165
 */

class geographic {

    function geographic() {
        
    }

    function getCity2() {
        global $db;
        $sql = "SELECT * FROM geographic WHERE parent_id IN (1,2,3) ORDER BY name";
        $rs = $db->query($sql);
        return $rs;
    }

    function getByParentId($id) {
        global $db;
        $sql = "SELECT * FROM geographic WHERE parent_id=$id ORDER BY name";
        $rs = $db->query($sql);

        return $rs;
    }

    function getLocation($param = null) {
        global $db;
        $sql = "SELECT * FROM location";
        $rs = $db->query($sql);
        $arrResult = array();
        while ($row = $db->FetchToArray($rs)) {
            $arrResult[] = $row;
        }
        return $arrResult;
    }

    function getWard($id) {
        global $db;
        $sql = "SELECT * FROM location_ward WHERE district_id=$id ORDER BY title";
        $rs = $db->query($sql);
        $arrResult = array();
        while ($row = $db->FetchToArray($rs)) {
            $arrResult[] = $row;
        }
        return $arrResult;
    }

    function getDistrict($city) {
        global $db;
        $sql = "SELECT * FROM location_district WHERE city_id=$city ORDER BY title";
        $rs = $db->query($sql);
        $arrResult = array();
        while ($row = $db->FetchToArray($rs)) {
            $arrResult[] = $row;
        }
        return $arrResult;
    }

    function getCity() {
        global $db;
        $sql = "SELECT * FROM location_city ORDER BY title";
        $rs = $db->query($sql);
        $arrResult = array();
        while ($row = $db->FetchToArray($rs)) {
            $arrResult[] = $row;
        }
        return $arrResult;
    }

    function merge() {
        global $db;
        $arrW = array();
        $arrD = array();
        $arrCity = $this->getCity();
        foreach ($arrCity as $city) {
            $arrDistrict = $this->getDistrict($city['id']);
            $distParentId = $city['id'];
            foreach ($arrDistrict as $district) {
                $distName = $district['title'] . " " . $city['title'];
                $sql = "INSERT INTO location (name,parent_id) VALUES ('" . $distName . "'," . $distParentId . ")";
                $rs = $db->query($sql);
                $wardParentId = $db->GetLastInsertID();
                $arrWard = $this->getWard($district['id']);
                foreach ($arrWard as $ward) {
                    $arrTemp = array();
                    $arrTemp['name'] = $ward['title'] . " " . $district['title'] . " " . $city['title'];
                    $arrTemp['parent_id'] = $wardParentId;
                    $arrW[] = $arrTemp;
                }
            }
        }
        foreach ($arrW as $ward) {
            $sql = "INSERT INTO location(name,parent_id) VALUES ('" . $ward['name'] . "'," . $ward['parent_id'] . ")";
            $db->query($sql);
        }
    }

    function tag_location() {
        global $db,$common;
        $arrLocation = $this->getLocation();
        foreach ($arrLocation as $location) {
            $tag=$common->nosignLink($location['name']);
            $sql = "UPDATE location set tag='".$tag."' WHERE id=".$location['id'];
            $db->query($sql);
        }
    }

}

?>
